<template>
  <div class="article">
    <div class="search">
      <span>笔记标题</span>
      <el-input v-model="searchVal" placeholder="请输入笔记标题" clearable></el-input>
      <el-button type="primary" @click="search">查询</el-button>
    </div>
    <el-divider></el-divider>
    <div style="text-align: left;"><el-button type="primary" @click="addDialog">新建</el-button></div>
    <!-- 列表 -->
    <el-table :data="tableData" style="width: 100%;">
      <el-table-column prop="title" label="笔记标题" width="180">
      </el-table-column>
      <el-table-column prop="content" label="笔记内容" width="380" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="editDialog(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
          <el-button @click="view(scope.row)" type="text" size="small">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="text-align: right;margin-top: 20px;">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 笔记 -->
    <el-dialog :title="articleTitle" :visible.sync="dialogFormVisible" @close="dialogClose">
      <el-form :model="form" ref="refform">
        <el-form-item label="笔记名称" :label-width="formLabelWidth" prop="title">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="笔记类容" :label-width="formLabelWidth" prop="content">
          <el-input type="textarea" v-model="form.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveHandle">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 查看 -->
    <el-dialog title="笔记详情" :visible.sync="dialogView">
      <div style="font-size: 16px;font-weight: 700;">{{ details.title }}</div>
      <div style="text-align: left;text-indent: 2em;margin-top: 30px;">{{ details.content }}</div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      dialogView:false,
      details:{
        title: '',
        content: ''
      },
      form: {
        title: '',
        content: ''
      },
      formLabelWidth: '120px',
      articleTitle: "",
      total:0,
      currentPage: 1,
      pageSize:5,
      searchVal:""
    }
  },
  mounted() {
    this.initList()
  },
  methods: {
    search(){
      this._axios.get(`/api/articles/?currentPage=${this.currentPage}&pageSize=${this.pageSize}&title=${this.searchVal}`).then((res) => {
        console.log(res, '笔记列表')
        if (res.data.code == 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    initList() {
      this._axios.get(`/api/articles/?currentPage=${this.currentPage}&pageSize=${this.pageSize}`).then((res) => {
        console.log(res, '笔记列表')
        if (res.data.code == 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    addDialog() {
      this.dialogFormVisible = true
      this.articleTitle = '新建笔记'
    },
    add() {
      this._axios.post('/api/articles', { ...this.form }).then((res) => {
        console.log(res, '新建笔记')
        if (res.data.code == 200) {
          this.$message({
            message: '笔记新建成功',
            type: 'success'
          });
          this.initList()
        }
        this.dialogFormVisible = false
      })
    },
    editDialog(row) {
      this.articleTitle = '编辑笔记'
      this.dialogFormVisible = true
      this.form = { ...row }
    },
    editClick() {
      this._axios.put(`/api/articles`, { ...this.form }).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: '笔记修改成功',
            type: 'success'
          });
          this.initList()
        }
        this.dialogFormVisible = false
      })
    },
    saveHandle() {
      if (this.articleTitle == '新建笔记') {
        this.add()
      } else {
        this.editClick()
      }
    },
    deleteClick(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this._axios.delete(`/api/articles/${row.id}`).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              message: '笔记删除成功',
              type: 'success'
            });
            this.initList()
          }
          this.dialogFormVisible = false
        })
      }).catch(() => {

      });

    },
    dialogClose() {
      // this.$refs.refform.resetFields();
      this.form.title = ""
      this.form.content = ""
    },
    view(row) {
      this._axios.get(`/api/articles/${row.id}`).then((res) => {
        if (res.data.code == 200) {
          console.log(res.data.data, 'view')
          this.dialogView = true
          this.details = res.data.data[0]
        }
        this.dialogFormVisible = false
      })
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val
      this.initList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val
      this.initList()
    }
  }
}
</script>
<style lang="scss">
.article {
  padding: 0 100px;
}
.search{
  display: flex;
  align-items: center;
  span{
    margin-right: 20px;
  }
  .el-input{
    width: 200px;
  }
  .el-button{
    width: 70px;
  }
 
}
</style>
